package dictcn.com.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.content.Intent;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import dictcn.com.R;
import dictcn.com.R.id;
import dictcn.com.R.layout;
import dictcn.com.control.MyDatabaseHelper;
import dictcn.com.control.MytabOperate;
import dictcn.com.model.Book;
import dictcn.com.model.LocalLibrary;

public class BookNameListShow extends Activity {//生词本中生词的信息基本显示activity

	private ListView listView=null;
	private SQLiteOpenHelper helper=null;
	private MytabOperate mytab=null;
	private List<Book> bookList=null;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.book_list);
		listView=(ListView)findViewById(R.id.lv_list_books);
		helper=new MyDatabaseHelper(BookNameListShow.this);
	    helper.getWritableDatabase();
		mytab=new MytabOperate(
		    	helper.getWritableDatabase());
		int b_id=getIntent().getIntExtra("b_id", 0);
		
		bookList=new ArrayList<Book>();
		bookList=mytab.SelctAllBook(String.valueOf(b_id));
		ArrayList<Map<String,String>> listItem=new ArrayList<Map<String,String>>();
		Map<String,String> map;
		for(int i=0;i<bookList.size();i++){
		  map=new HashMap<String,String>();
		  map.put("book_name", bookList.get(i).getL_id()+"");
		  map.put("book_interiptor",bookList.get(i).getT_time());
		  listItem.add(map);
		 }	         
	     SimpleAdapter simpleAdapter=new SimpleAdapter(BookNameListShow.this,
	       		listItem,
	       		R.layout.book_list_item,
	       		new String[]{"book_name","book_interiptor"},
	       		new int[]{R.id.txt_book_item,R.id.txt_book_interiptor}
	     );         
	    listView.setAdapter(simpleAdapter);
	    listView.setOnItemClickListener(new OnItemClickListenerImpl());

	}
	
	//每个生词的点击详细显示
	private class OnItemClickListenerImpl implements OnItemClickListener{
		
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			// TODO Auto-generated method stub
			
			//查询生词的操作数据类
			helper=new MyDatabaseHelper(BookNameListShow.this);
		    helper.getWritableDatabase();
			mytab=new MytabOperate(
			    	helper.getWritableDatabase());
			LocalLibrary local=mytab.findLocalLibrary(String.valueOf(bookList.get(arg2).getL_id()));
			
			Intent intent=new Intent();
			Bundle bundle=new Bundle();
			bundle.putSerializable("word", local);
			intent.putExtra("bundle", bundle);
			intent.setClass(BookNameListShow.this, ShowWordContent.class);
			startActivity(intent);	
		}
	}
	
	//返回按钮的监听事件
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if(keyCode==KeyEvent.KEYCODE_BACK)
		{
			BookNameListShow.this.finish();
		}
		return super.onKeyDown(keyCode, event);
	}
}
